<?php

class Register extends Controller {

    public $message;
    public $userReg;
    public $month;
    public $year;
    public $day;

    public function __construct() {
        parent::__construct();
        $this->_intialize();
    }

    public function __destruct() {
        parent::__destruct();
    }

    public function index() {
    	$this->library('validation');
    	$this->library('text');
        if (isset($this->_post['user'])) {
            $this->library('datetimex');
            $this->userReg = $this->_post['user'];
            //Clone post out...
            $tempReg = $this->_post['user'];
            //Create birthday from day,month, year.
            $this->userReg['birthday'] = mktime(0, 0, 0, $this->userReg['month'], $this->userReg['day'], $this->userReg['year']);
            //Unset value not neccessary for create new user.
            foreach (array('day', 'month', 'year', 'register', 'captcha') as $val) {
                unset($this->userReg[$val]);
            }

            $this->model('user', '', $this->userReg);
            if($tempReg['month'] != -1){
                $days = $this->_getDays($tempReg);
                $this->_setDays($days);
            }
            $this->error = $this->user->registration($tempReg['captcha']);
            $this->userReg = (object) $tempReg;
        }

        $this->template('register');
    }

    private function _intialize() {
        $this->error = '';
        $this->day[-1] = 'Day:';
        for ($i = 1; $i <= 31; $i++) {
            $this->day[$i] = $i;
        }

        $this->month = array(
            -1 => 'Month:',
            1 => 'Jan',
            2 => 'Feb',
            3 => 'Mar',
            4 => 'Apr',
            5 => 'May',
            6 => 'Jun',
            7 => 'July',
            8 => 'Aug',
            9 => 'Sep',
            10 => 'Oct',
            11 => 'Nov',
            12 => 'Dec'
        );

        $currentYear = date('Y');
        $this->year[-1] = 'Year:';
        for ($i = $currentYear; $i >=1905; $i--) {
        //for ($i = 1905; $i <= $currentYear; $i++) {
            $this->year[$i] = $i;
        }
    }

    public function confirm() {
        if (!empty($this->_get['hash']) && !empty($this->_get['account'])) {
            $this->model('user');
            $this->message = $this->user->activeAccount($this->_get['account'], $this->_get['hash']);
            $this->template('active_account');
            return;
        }

        $this->message = 'You input a wrong address';
        $this->template('active_account');
    }

    public function createcaptcha() {
        $this->library('captcha');
        $this->library('text');
        $captcharName = 'secure';
        //$_SESSION[$captcharName] = substr(md5(rand(0, 999999)), 20,5);
        $_SESSION[$captcharName] = strtolower(text::randomString());
        //$_SESSION[$captcharName] =  rand(100000, 999999);
        $this->captcha->create($_SESSION[$captcharName]);
    }

    private function _getDays1($tempReg) {
        $numDays = 0;
        if ($tempReg['month'] == 2 && isset($tempReg['year'])) {
            $numDays = Datetimex::getDaysInMonth($tempReg['month'], $tempReg['year']);
        } else
        if ($tempReg['month'] == 2) {
            $numDays = 28;
        } else {
            $numDays = Datetimex::getDaysInMonth($tempReg['month'], 0);
        }

        return $numDays;
    }

    private function _getDays($tempReg) {
        return ($tempReg['month'] == 2) ?
            ($tempReg['year'] != -1 ? Datetimex::getDaysInMonth($tempReg['month'], $tempReg['year']) : 29) :
            Datetimex::getDaysInMonth($tempReg['month'], 0);
    }

    private function _setDays($days){
        for ($i = 31; $i > $days; $i--) {
            unset($this->day[$i]);
        }
    }
}

?>
